package com.yl.zqy;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import lombok.extern.slf4j.Slf4j;

import java.util.*;

@Slf4j
public class CodeGenerator {
    private static final String projectPath = "E:\\work\\self\\zqy-admin\\src\\main\\java";
    private static final String controllerPathFormat = "E:\\work\\self\\zqy-admin\\src\\main\\java\\com\\yl\\zqy\\controller\\%s";
    private static final String entityPathFormat = "E:\\work\\self\\zqy-admin\\src\\main\\java\\com\\yl\\zqy\\entity\\%s";
    private static final String servicePathFormat = "E:\\work\\self\\zqy-admin\\src\\main\\java\\com\\yl\\zqy\\service\\%s";
    private static final String serviceImplPathFormat = "E:\\work\\self\\zqy-admin\\src\\main\\java\\com\\yl\\zqy\\service\\impl\\%s";
    private static final String mapperPathFormat = "E:\\work\\self\\zqy-admin\\src\\main\\java\\com\\yl\\zqy\\mapper\\%s";
    private static final String xmlPathFormat = "E:\\work\\self\\zqy-admin\\src\\main\\resources\\mapper\\%s";
    private static final String author = "liang.yu";
    private static final String packageParent = "com.yl.zqy";
    private static final String url = "jdbc:mysql://localhost:3306/zqy-admin?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true";
    private static final String user = "root";
    private static final String password = "123456";


    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        log.debug("请输入业务模块名：");
        String modelName = sc.nextLine();
        Map<OutputFile, String> pathInfo = new HashMap<>();
        pathInfo.put(OutputFile.controller, String.format(controllerPathFormat, modelName));
        pathInfo.put(OutputFile.entity, String.format(entityPathFormat, modelName));
        pathInfo.put(OutputFile.service, String.format(servicePathFormat, modelName));
        pathInfo.put(OutputFile.serviceImpl, String.format(serviceImplPathFormat, modelName));
        pathInfo.put(OutputFile.mapper, String.format(mapperPathFormat, modelName));
        pathInfo.put(OutputFile.xml, String.format(xmlPathFormat, modelName));

        FastAutoGenerator.create(url, user, password)
                // 全局配置
                .globalConfig((scanner, builder) ->
                        builder.author(author)
                                .enableSwagger()
                                .outputDir(projectPath)
                )
                // 包配置
                .packageConfig((scanner, builder) -> builder
                        .parent(packageParent)
                        //.moduleName(scanner.apply("请输入模块名："))
                        .mapper("mapper." + modelName)
                        .xml("mapper." + modelName)
                        .entity("entity." + modelName)
                        .service("service." + modelName)
                        .serviceImpl("service.impl." + modelName)
                        .controller("controller." + modelName)
                        .pathInfo(pathInfo)
                )
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入" +
                                " all")))
                        // .controllerBuilder().enableRestStyle().enableHyphenStyle().fileOverride()
                        .entityBuilder().enableLombok().enableTableFieldAnnotation().fileOverride()
                        .serviceBuilder().formatServiceFileName("%sService").fileOverride().fileOverride()
                        .mapperBuilder().enableBaseColumnList().enableBaseResultMap().fileOverride().enableMapperAnnotation()
                        .build())
                .templateConfig((scanner, builder) -> builder
                        .entity("/templates/entity.java")
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
